<!--
 * @Author: ZhaoZhiqi
 * @Date: 2024-04-30 18:22:50
 * @LastEditors: ZhaoZhiqi
 * @LastEditTime: 2024-06-14 17:27:30
 * @Description: 车牌管理
 * @FilePath: \parking-mini\src\pages\numberplate.vue
-->
<template>
    <div class="container">
        <div class="title">已绑定{{ list.length }}个 <span>最多绑定3个</span></div>
        <div class="card" v-for="item in list" :key="item.id" :style="{ backgroundImage: `url(${imgs['numberplate_type' + item.carType]})` }">
            <div class="number">{{ item.carNumber }}</div>
            <img :src="imgs.delete" @click="remove(item)" />
        </div>
        <div v-if="list.length < 3" class="btn" @click="visible = true">
            <img :src="imgs.plus" />
            <span>添加车牌</span>
        </div>

        <u-overlay :show="visible">
            <div class="plate-container">
                <!-- 输入区域 -->
                <div class="plate-input">
                    <div class="plate-input-header">
                        添加车牌
                        <img :src="imgs.close" @click="visible = false" />
                    </div>
                    <div class="plate-input-content">
                        <div class="plate-input-label">请输入车牌号码</div>
                        <div class="plate-input-list">
                            <div class="plate-input-item" :class="{ current: index === inputIndex, dashed: index === 7 }" v-for="(item, index) in numberArray" :key="index">
                                {{ item }}
                                <div v-if="index === 7 && !item">新能源</div>
                            </div>
                        </div>
                        <button class="plate-input-button" :class="{ disabled: !numberArray[6] }" @click="confirm">确认添加</button>
                    </div>
                </div>
                <!-- 键盘区域 -->
                <div class="plate-content">
                    <!-- 数字键盘 -->
                    <template v-if="plateIndex === 0">
                        <div class="plate-row">
                            <button class="plate-btn" v-for="item in toArray(row1)" :key="item" @click="() => select(item)">{{ item }}</button>
                        </div>
                        <div class="plate-row">
                            <button class="plate-btn" v-for="item in toArray(row2)" :key="item" @click="() => select(item)">{{ item }}</button>
                        </div>
                        <div class="plate-row">
                            <button class="plate-btn" v-for="item in toArray(row3)" :key="item" @click="() => select(item)">{{ item }}</button>
                        </div>
                        <div class="plate-row">
                            <button class="plate-btn plate-btn-gray" @click="plateIndex = 1">地区</button>
                            <button class="plate-btn" v-for="item in toArray(row4)" :key="item" @click="() => select(item)">{{ item }}</button>
                            <button class="plate-btn plate-btn-gray" @click="() => backspace()">
                                <img :src="imgs.backspace" />
                            </button>
                        </div>
                    </template>
                    <!-- 地区键盘 -->
                    <template v-else-if="plateIndex === 1">
                        <div class="plate-row">
                            <button class="plate-btn" v-for="item in toArray(row5)" :key="item" @click="() => select(item)">{{ item }}</button>
                        </div>
                        <div class="plate-row">
                            <button class="plate-btn" v-for="item in toArray(row6)" :key="item" @click="() => select(item)">{{ item }}</button>
                        </div>
                        <div class="plate-row">
                            <button class="plate-btn" v-for="item in toArray(row7)" :key="item" @click="() => select(item)">{{ item }}</button>
                        </div>
                        <div class="plate-row">
                            <button class="plate-btn plate-btn-gray" @click="plateIndex = 0">ABC</button>
                            <button class="plate-btn" v-for="item in toArray(row8)" :key="item" @click="() => select(item)">{{ item }}</button>
                            <button class="plate-btn plate-btn-gray" @click="() => backspace()">
                                <img :src="imgs.backspace" />
                            </button>
                        </div>
                    </template>
                </div>
            </div>
        </u-overlay>
    </div>
</template>
<script>
import { getPlateListAPI, addPlateAPI, removePlateAPI } from "../api/api";
const app = getApp();
export default {
    data() {
        return {
            imgs: app.globalData.imgs,
            visible: false,

            // 数字键盘
            row1: "1234567890",
            row2: "QWERTYUIOP",
            row3: "ASDFGHJKL",
            row4: "ZXCVBNM",

            // 地区键盘
            row5: "京沪粤津冀晋蒙辽吉黑",
            row6: "苏浙皖闽赣鲁豫鄂湘桂",
            row7: "琼渝川贵云藏陕甘青宁",
            row8: "新使领警学港澳",

            list: [],

            plateIndex: 0,
            inputIndex: 1,
            numberArray: ["湘", "", "", "", "", "", "", ""],
            loading: false,
        };
    },
    onShareAppMessage() {},
    onLoad() {
        this.query();
    },
    methods: {
        toArray(string) {
            return string.split("");
        },
        // 查询车牌列表
        query() {
            wx.showLoading({
                title: "加载中...",
            });
            getPlateListAPI({
                openId: wx.getStorageSync("parking_openid"),
            }).then((res) => {
                this.list = res;
                wx.hideLoading();
            });
        },
        // 点击键盘
        select(item) {
            this.numberArray[this.inputIndex] = item;
            if (this.inputIndex < 7) {
                this.inputIndex++;
            } else if (this.inputIndex === 7) {
                this.numberArray[this.inputIndex] = item;
                this.$forceUpdate();
            }
            if (this.inputIndex > 0) {
                this.plateIndex = 0;
            }
        },
        // 退格
        backspace() {
            this.numberArray[this.inputIndex] = "";
            if (this.inputIndex > 0) {
                this.inputIndex--;
            }
            if (this.inputIndex === 0) {
                this.plateIndex = 1;
            }
        },
        // 确定创建车牌
        confirm() {
            if (this.loading) return false;
            if (!this.numberArray[6]) return false;
            let carNumber = this.numberArray.join("");
            this.loading = true;
            wx.showLoading({
                title: "添加中...",
            });
            addPlateAPI({
                openId: wx.getStorageSync("parking_openid"),
                token: wx.getStorageSync("dada_token"),
                carNumber,
            })
                .then((res) => {
                    wx.showToast({
                        title: "添加成功",
                        icon: "success",
                    });
                    this.loading = false;
                    this.visible = false;
                    this.numberArray = ["湘", "", "", "", "", "", "", ""];
                    this.plateIndex = 0;
                    this.inputIndex = 1;
                    this.query();
                })
                .catch((err) => {
                    this.loading = false;
                });
        },
        // 删除车牌
        remove(item) {
            wx.showModal({
                title: "确定删除吗?",
                content: `确定删除 ${item.carNumber} 吗？`,
                success: (res) => {
                    if (res.confirm) {
                        wx.showLoading({
                            title: "删除中...",
                        });
                        removePlateAPI({
                            id: item.id,
                            token: wx.getStorageSync("dada_token"),
                        }).then((res) => {
                            wx.hideLoading();
                            this.query();
                        });
                    } else if (res.cancel) {
                        console.log("用户点击取消");
                    }
                },
            });
        },
    },
};
</script>
<style scoped lang="scss">
.container {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 40rpx;
    .title {
        padding-left: 32rpx;
        margin-bottom: 32rpx;
        font-size: 32rpx;
        line-height: 1;
        color: #000000;
        align-self: flex-start;
        font-weight: bold;
        span {
            color: #4d4d4d;
            font-size: 22rpx;
            line-height: 1;
            margin-left: 13rpx;
        }
    }
    .card {
        width: 686rpx;
        height: 156rpx;
        border-radius: 24rpx;
        margin-bottom: 32rpx;
        background-repeat: no-repeat;
        background-position: center;
        background-size: cover;
        display: flex;
        align-items: center;
        .number {
            flex: auto;
            font-size: 48rpx;
            line-height: 1;
            margin-left: 150rpx;
            font-weight: bold;
            color: #fff;
        }
        img {
            flex: none;
            width: 30rpx;
            height: 32rpx;
            margin-right: 40rpx;
        }
    }
    .btn {
        width: 686rpx;
        height: 156rpx;
        box-sizing: border-box;
        border: 4rpx dashed #cccccc;
        border-radius: 24rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 30rpx;
        color: #0081fe;
        font-weight: bold;
        img {
            width: 25rpx;
            height: 25rpx;
            margin-right: 15rpx;
        }
    }
}
.plate-input {
    position: absolute;
    bottom: 600rpx;
    width: 686rpx;
    left: calc((750rpx - 686rpx) / 2);
    border-radius: 24rpx;
    background-color: #fff;
    overflow: hidden;
    .plate-input-header {
        background-color: #f5f5f5;
        color: #000;
        font-size: 32rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
        height: 100rpx;
        font-weight: bold;
        img {
            width: 30rpx;
            height: 30rpx;
            position: absolute;
            top: 35rpx;
            right: 30rpx;
            z-index: 2;
        }
    }
    .plate-input-content {
        padding: 20rpx 0 50rpx;
        display: flex;
        flex-direction: column;
        align-items: center;
        .plate-input-label {
            width: 100%;
            color: #000;
            font-size: 30rpx;
            line-height: 1;
            font-weight: bold;
            margin: 20rpx 0;
            padding-left: 40rpx;
            box-sizing: border-box;
        }
        .plate-input-list {
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 30rpx 0;
            .plate-input-item {
                display: flex;
                justify-content: center;
                align-items: center;
                margin: 0 8rpx;
                width: 60rpx;
                height: 80rpx;
                border-radius: 10rpx;
                border: 1rpx solid #ccc;
                &.dashed {
                    border: 1rpx dashed #ccc;
                    div {
                        font-size: 18rpx;
                        color: #999;
                    }
                }
                &.current {
                    border: 1rpx solid #6794f6;
                }
            }
        }
        .plate-input-button {
            width: 90%;
            background-color: #6794f6;
            color: #fff;
            height: 100rpx;
            border-radius: 50rpx;
            display: flex;
            justify-content: center;
            align-items: center;
            margin: 20rpx 0;
            &.disabled {
                background-color: #999;
            }
        }
    }
}
.plate-content {
    width: 100%;
    position: absolute;
    bottom: 0;
    background-color: #d4d7de;
    padding: 20rpx 0 50rpx;
    border-top-left-radius: 12rpx;
    border-top-right-radius: 12rpx;
    .plate-row {
        display: flex;
        justify-content: center;
        .plate-btn {
            padding: 0;
            font-size: 32rpx;
            display: flex;
            align-items: center;
            justify-content: center;
            width: 60rpx;
            height: 80rpx;
            margin: 10rpx 6rpx;
            background-color: #fff;
            color: #333;
            border-radius: 12rpx;
            border-bottom: 1rpx solid #666;
            &.plate-btn-gray {
                width: 90rpx;
                background-color: #b1b8c8;
                font-size: 28rpx;
            }
            &:active {
                transform: translateY(4rpx);
            }
            img {
                width: 40rpx;
                height: 40rpx;
            }
        }
    }
}
</style>
